<?php
require_once 'MyPdo.class.php';
class zheng_hou{

	private $p = null;
	private $table = 'bianzheng_zhenghou';
	function __construct(){
		if(!$this->p){
			$this->p =  MyPdo::init();
		}
	}


	//删除征候下的症状
	function _delZhengHouSubZhengZhuang($id,$hid){
		$sql = "select list from {$this->table} where id = ?";
		$res = $this->p->dql($sql,array($hid),1);

		$arr1 = json_decode($res['list'],1);
		$arr2 = array();
		foreach($arr1 as $v){
			if($v != $id){
				$arr2[] = intval($v);
			}
		}
		$sql2 = "update {$this->table} set list = ? where id = ?";
		return $this->p->dml($sql2,array(json_encode($arr2),$hid));
	}



	//给征候添加症状
	function _zhengHouAddZhengZhuang($id,$list){
		$sql = "select list from {$this->table} where id = ?";
		$res = $this->p->dql($sql,array($id),1);
		$arr1 = explode(',',$list);
		$arr2 = json_decode($res['list'],1);
		$arr3 = array_merge($arr2,$arr1);
		$arr4 = array_unique($arr3);
		foreach($arr4 as $v){
			$arr5[] = intval($v);
		}
		$sql2 = "update {$this->table} set list = ? where id = ?";
		return $this->p->dml($sql2,array(json_encode($arr5),$id));
	}


	//征候删除
	function _zhengHouDel($id){
		$sql = "delete from {$this->table} where id = ?";
		return $this->p->dml($sql,array($id));
	}


	//征候重命名
	function _zhengHouRname($id,$name){
		$sql = "update {$this->table} set name = ? where id = ?";
		return $this->p->dml($sql,array($name,$id));
	}


	//征候上移下移
	function _zhengHou_upDown($id,$ctype){
		if($ctype == 'up'){
			$sql = "update {$this->table} set orders = orders+1 where id = ?";
		}else{
			$sql = "update {$this->table} set orders = if(orders-1<1,1,orders-1) where id = ?";
		}
		return $this->p->dml($sql,array($id));
	}




	//获取征候列表
	function _getZhengHouList($ctype){
		$sql = "select * from {$this->table} where type=? order by orders";
		$res = $this->p->dql($sql,array($ctype));
		if(!$res){
			return false;
		}
		foreach($res as $k => $v){
			$list = implode(',',json_decode($v['list']));
			if($list){
				$sql = "select id,name,orders from bianzheng_zhengzhuang where id in({$list}) order by orders";
				$res2 = $this->p->dql($sql,array());
				if($res2){
					$res[$k]['subList'] = $res2;
				}else{
					$res[$k]['subList'] = array();
				}
				unset($res[$k]['list']);
			}
		}
		return $res;
	}




	//添加征候
	function _addZhengHou($name,$ctype){
		$sqlCheck = "select id from {$this->table} where name = ?";
		$resCheck = $this->p->dql($sqlCheck,array($name),1);
		if($resCheck){
			return false;
		}
		$sqlMax = "select max(orders) max from {$this->table} where type = ?";
		$resMax = $this->p->dql($sqlMax,array($ctype),1);
		$max = !$resMax['max']?1:$resMax['max']+1;

		$sql = "insert into {$this->table} values(null,?,?,?,$max)";
		return $this->p->dml($sql,array($name,json_encode(array()),$ctype));
	}



}
